/**
 * 将整数部分逢三一断
 * @param value
 * @return string
 */
export const NumberFormat = (value?: string | number): string => {
    return value ? value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') : '0';
};

export const FormatWan = (val: number): [number, string] | null => {
    const v = val * 1;
    if (!v || Number.isNaN(v)) return null;
    let result = [val, ''] as [number, string];
    if (val > 10000) {
        result = [Math.floor(val / 10000), '万'];
    }
    return result;
};

const fraction = ['角', '分'];
const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
const unit = [
    ['圆', '万', '亿'],
    ['', '拾', '佰', '仟'],
];

export const number2upper = (money: number) => {
    let num = Math.abs(money);
    let s = '';
    fraction.forEach((item, index) => {
        s += (digit[Math.floor(num * 10 * 10 ** index) % 10] + item).replace(/零./, '');
    });
    s = s || '整';
    num = Math.floor(num);
    for (let i = 0; i < unit[0].length && num > 0; i += 1) {
        let p = '';
        for (let j = 0; j < unit[1].length && num > 0; j += 1) {
            p = digit[num % 10] + unit[1][j] + p;
            num = Math.floor(num / 10);
        }
        s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
    }

    return s
        .replace(/(零.)*零圆/, '圆')
        .replace(/(零.)+/g, '零')
        .replace(/^整$/, '零圆整');
};
